/*
 * Copyright (C) 2003, Velocityme Partnership
 * GuiDisplayCompletedTasks.java
 *
 * Created on 04 October 2004, 07:46
 */

package com.velocityme.client.gui.search;

import com.velocityme.client.gui.GuiDisplaySearchResults;
import com.velocityme.client.gui.calendar.CalendarDatePanel;
import com.velocityme.client.gui.exception.ExceptionWindow;
import com.velocityme.client.utility.StatelessRemoteClientSessionUtil;
import com.velocityme.interfaces.KeySession;
import com.velocityme.interfaces.NodePK;
import com.velocityme.utility.InvalidKeyException;
import java.awt.Component;
import java.io.ByteArrayOutputStream;
import java.rmi.RemoteException;
import java.util.Collection;
import java.util.Date;
import javax.ejb.CreateException;
import javax.naming.NamingException;
import javax.swing.JTabbedPane;

/**
 *
 * @author  Robert
 */
public class GuiDisplayCompletedTasks extends javax.swing.JPanel {
    
    private KeySession m_key;
    private NodePK m_rootNodePK;
    private CalendarDatePanel m_datePanel;
    private ByteArrayOutputStream m_currentHtmlOutputStream;
    
    /** Creates new form GuiDisplayCompletedTasks */
    public GuiDisplayCompletedTasks(KeySession p_key, NodePK p_rootNodePK) {
        initComponents();
        m_key = p_key;
        m_rootNodePK = p_rootNodePK;
        m_datePanel = new CalendarDatePanel();
        java.awt.GridBagConstraints gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel2.add(m_datePanel, gridBagConstraints);
        Date lastWeek = new Date();
        lastWeek.setTime(lastWeek.getTime() - 7 * 24 * 60 * 60 * 1000);
        m_datePanel.setDate(lastWeek);
        try {
            jTextFieldRoot.setText(StatelessRemoteClientSessionUtil.getSession().getNodePathName(m_key, m_rootNodePK));
        }
        catch(NamingException e) {
            ExceptionWindow.showError(e);
        }
        catch(CreateException e) {
            ExceptionWindow.showError(e);
        }
        catch(RemoteException e) {
            ExceptionWindow.showError(e);
        }
        catch(InvalidKeyException e) {
            ExceptionWindow.showError(e);
        }
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        java.awt.GridBagConstraints gridBagConstraints;

        jPanel2 = new javax.swing.JPanel();
        jLabelRoot = new javax.swing.JLabel();
        jTextFieldRoot = new javax.swing.JTextField();
        jLabelStart = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jButtonSearch = new javax.swing.JButton();
        jButtonClose = new javax.swing.JButton();

        setLayout(new java.awt.BorderLayout());

        jPanel2.setLayout(new java.awt.GridBagLayout());

        jPanel2.setMinimumSize(new java.awt.Dimension(0, 0));
        jLabelRoot.setText("Search Root:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        jPanel2.add(jLabelRoot, gridBagConstraints);

        jTextFieldRoot.setEditable(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.weightx = 1.0;
        jPanel2.add(jTextFieldRoot, gridBagConstraints);

        jLabelStart.setText("Start:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        jPanel2.add(jLabelStart, gridBagConstraints);

        add(jPanel2, java.awt.BorderLayout.NORTH);

        jPanel3.setLayout(new java.awt.GridLayout());

        jButtonSearch.setText("Search");
        jButtonSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSearchActionPerformed(evt);
            }
        });

        jPanel3.add(jButtonSearch);

        jButtonClose.setText("Close");
        jButtonClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCloseActionPerformed(evt);
            }
        });

        jPanel3.add(jButtonClose);

        add(jPanel3, java.awt.BorderLayout.SOUTH);

    }//GEN-END:initComponents

    private void jButtonCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCloseActionPerformed
        // TODO add your handling code here:
        getParent().remove(this);        
    }//GEN-LAST:event_jButtonCloseActionPerformed

    private void jButtonSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSearchActionPerformed
        // TODO add your handling code here:
        try {
            Date date = m_datePanel.getDate();
            Collection results = StatelessRemoteClientSessionUtil.getSession().findCompletedTasks(m_key, m_rootNodePK, date);
            Component component = new GuiDisplaySearchResults (m_key, m_rootNodePK, results);
            JTabbedPane tabbedPane = (JTabbedPane)getParent();
            tabbedPane.addTab("Search Results", component);
            tabbedPane.setSelectedComponent(component);
        }
        catch(Exception e) {
            ExceptionWindow.showError(e);
        }        
    }//GEN-LAST:event_jButtonSearchActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonClose;
    private javax.swing.JButton jButtonSearch;
    private javax.swing.JLabel jLabelRoot;
    private javax.swing.JLabel jLabelStart;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JTextField jTextFieldRoot;
    // End of variables declaration//GEN-END:variables
    
}
